package com.cn.项目.飞机预约管理系统.Maininterface;

import com.cn.项目.utils.JdbcUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Flight extends JPanel {

    public Flight(){
        JPanel p1 = new JPanel();
        p1.setLayout(new BorderLayout());
        JPanel p2 = new JPanel();
        JLabel jLabel = new JLabel("设置航班");
        jLabel.setFont(new Font("华文楷书", 1, 30));
        jLabel.setForeground(Color.red);
        p2.add(jLabel,JLabel.CENTER);
        p1.add(p2,"North");

        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(6,2,30,10 ));
        p1.add(p3,"Center");

        JLabel jLabel4 = new JLabel("出发日期：");
        p3.add(jLabel4);
        JComboBox travel = new JComboBox();
        travel.addItem("2021-12-13");
        travel.addItem("2021-12-14");
        travel.addItem("2021-12-15");
        travel.addItem("2021-12-16");
        travel.addItem("2021-12-17");
        travel.addItem("2021-12-18");
        travel.addItem("2021-12-19");
        travel.addItem("2021-12-20");
        final String[] date = {null};
        travel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                date[0] = travel.getSelectedItem().toString();
            }
        });
        p3.add(travel);
        JLabel jLabel1 = new JLabel("出发地点：");
        p3.add(jLabel1);
        JComboBox start = new JComboBox();
        start.addItem("上海");
        start.addItem("北京");
        start.addItem("广州");
        start.addItem("深圳");
        p3.add(start);
        final String[] startCity = {null};
        start.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                 startCity[0] = start.getSelectedItem().toString();
            }
        });


        JLabel jLabe2 = new JLabel("出发时间：");
        p3.add(jLabe2);
        JComboBox sTime = new JComboBox();
        p3.add(sTime);
        sTime.addItem("8:00");
        sTime.addItem("10:00");
        sTime.addItem("12:00");
        sTime.addItem("14:00");
        sTime.addItem("16:00");
        sTime.addItem("18:00");
        final String[] startTime = {null};
        sTime.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startTime[0] = sTime.getSelectedItem().toString();
            }
        });


        JLabel jLabel2 = new JLabel("目的地：");
        p3.add(jLabel2);

        JComboBox end = new JComboBox();
        end.addItem("上海");
        end.addItem("洛杉矶");
        end.addItem("香港");
        end.addItem("濮阳");
        end.addItem("北京");
        end.addItem("澳门");
        p3.add(end);
        final String[] endCity = {null};
                end.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                endCity[0] = end.getSelectedItem().toString();

            }
        });



        JLabel jLabel3 = new JLabel("到达时间：");
        p3.add(jLabel3);
        JComboBox eTime = new JComboBox();
        eTime.addItem("8:00");
        eTime.addItem("10:00");
        eTime.addItem("12:00");
        eTime.addItem("14:00");
        eTime.addItem("16:00");
        eTime.addItem("18:00");
        p3.add(eTime);
        final String[] endTime = {null};
                eTime.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                endTime[0] = eTime.getSelectedItem().toString();
            }
        });
        JLabel jLabel5 = new JLabel("设定价格：");
        p3.add(jLabel5);
        JComboBox money = new JComboBox();
        money.addItem("100");
        money.addItem("200");
        money.addItem("300");
        money.addItem("450");
        money.addItem("550");
        money.addItem("750");
        money.addItem("850");
        p3.add(money);
        final String[] fmoney = {null};
        money.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              fmoney[0] =   money.getSelectedItem().toString();
            }
        });

        JPanel p4 = new JPanel();
        p1.add(p4,"South");
        JButton jButton = new JButton("设置");
        p4.add(jButton);
        //设置监听
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection conn = null;
                PreparedStatement st = null;
                ResultSet rs= null;
                try {
                   conn =  JdbcUtils.getConnection();
                   String sql = "insert into training.flight(traveldate,start, stime, `end`, etime,fmoney) values (?,?,?,?,?,?);";
                   st  = conn.prepareStatement(sql);
                   st.setString(1, date[0]);
                   st.setString(2, startCity[0]);
                   st.setString(3, startTime[0]);
                   st.setString(4, endCity[0]);
                   st.setString(5, endTime[0]);
                   st.setString(6, fmoney[0]);
                    int i = st.executeUpdate();
                    if (i>0){
                        new Success();
                    }else {
                        new Fail();
                    }
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }finally {
                    JdbcUtils.release(conn,st ,rs );

                }
            }
        });


        this.add(p1);
        this.setVisible(true);
    }
}
